package com.jims.master.modules.utils;

import com.jims.master.common.utils.SpringContextHolder;
import com.jims.master.modules.csm.dao.*;
import com.jims.master.modules.csm.entity.*;
import com.jims.master.modules.csm.service.ExpBuyPlanService;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CTQ on 2017-03-14 11:20:04
 */
public class ExpUtils {

    private static ExpStorageDeptDao expStorageDeptDao = SpringContextHolder.getBean(ExpStorageDeptDao.class);
    private static ExpInoutClassDao expInoutClassDao = SpringContextHolder.getBean(ExpInoutClassDao.class);
    private static ExpSupplierCataDao expSupplierCataDao = SpringContextHolder.getBean(ExpSupplierCataDao.class);
    private static ExpImportMasterDao expImportMasterDao = SpringContextHolder.getBean(ExpImportMasterDao.class);
    private static ExpBuyPlanDao expBuyPlanDao = SpringContextHolder.getBean(ExpBuyPlanDao.class);
    public static final String CACHE_DICT_MAP = "expStorageMap";

    public static List<Dict> list = DictUtils.getDictList("EXP_STOTARG_TYPE");

    /**
     * 根据传递过来的入出库区分查询入(出)库类别
     * @param inoutType 入库 1、出库 0
     * @return
     * @author fengyg
     */
    public static List<ExpInoutClass> getImpOrOutpList(String inoutType) {
        //获取当前库房级别
        String storageLevel = "";
        ExpStorageDept storageDept = expStorageDeptDao.get(UserUtils.getUser().getOrgDept().getId());
        if(null != storageDept){
            storageLevel = storageDept.getStorageLevel();
            ExpInoutClass expInoutClass = new ExpInoutClass();
        expInoutClass.setInoutType(inoutType);
        expInoutClass.setStorageType((Integer.parseInt(storageLevel) + 1) + "");
        List<ExpInoutClass> list = expInoutClassDao.findByStorageLevelInOut(expInoutClass);
            return list;
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 获取科室的供货方集合:科室是三级库房,此功能是查询一级和二级库房的集合;科室是二级库房,则查询一级库集合
     *
     * @return
     * @description 查阅本库房所提申请功能使用
     * @author fengyg
     * @date 2016-06-16
     */
    public static List<ExpStorageDept> getDeptSupplierList() {
        //获取当前库房级别
        String storageLevel = "";
        ExpStorageDept expStorageDept = expStorageDeptDao.get(UserUtils.getUser().getOrgDept().getId());
        if(null != expStorageDept){
            storageLevel = expStorageDept.getStorageLevel();
        }else{
            return new ArrayList<ExpStorageDept>();
        }
        List<ExpStorageDept> oneStorageList = getByStorageLevel("0");  //一级库房
        List<ExpStorageDept> twoStorageList = getByStorageLevel("1");  //二级库房
        if (storageLevel.equals("1")) {  //库房级别"1",二级库房
            return oneStorageList;   //供货方为一级库
        } else {   //库房级别"2",三级科室库
            twoStorageList.addAll(oneStorageList);  //供货方为一二级库房
            return twoStorageList;
        }
    }

    /**
     * 获取申请科室集合:当前科室为二级科室,申请科室是二级科室和三级科室的集合
     *
     * @return
     * @description 科室申请审核功能使用
     * @author fengyg
     * @date 2017-06-19
     */
    public static List<ExpStorageDept> getApplyList() {
        List<ExpStorageDept> twoStorageList = getByStorageLevel("1");  //二级库房
        List<ExpStorageDept> threeStorageList = getByStorageLevel("2");  //三级科室库房
        twoStorageList.addAll(threeStorageList);
        return twoStorageList;
    }

    /**
     * 获取科室的供货方集合（科室申请审核功能使用）
     *
     * @return 一级库、二级库的集合
     * @author fengyg
     * @date 2017-06-19
     */
    public static List<ExpStorageDept> getAuditSupplyList() {
        List<ExpStorageDept> oneStorageList = getByStorageLevel("0");  //一级库房
        List<ExpStorageDept> twoStorageList = getByStorageLevel("1");  //二级库房
        oneStorageList.addAll(twoStorageList);
        return oneStorageList;
    }

    /**
     * 根据ID获取生产商(供应商)
     * @param id
     * @return
     * @author fengyg
     */
    public static String getExpSupplierById(String id) {
        if (null != id && !id.trim().equals("")) {
            ExpSupplierCata expSupplierCata=expSupplierCataDao.get(id);
            if(expSupplierCata!=null){
                return expSupplierCata.getSupplierJc();
            }
            else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 厂家翻译
     *
     * @return
     * @author fengyg
     */
    public static List<ExpSupplierCata> getExpSupplierList() {
        ExpSupplierCata expSupplierCata = new ExpSupplierCata();
        expSupplierCata.setDelFlag("0");
        return expSupplierCataDao.findList(expSupplierCata);
    }

    /**
     * 根据库房级别获取该级别的库房集合
     *
     * @param storageLevel
     * @return
     * @author fengyg
     */
    public static List<ExpStorageDept> getByStorageLevel(String storageLevel) {
        ExpStorageDept storageDept = new ExpStorageDept();
        storageDept.setStorageLevel(storageLevel);
        storageDept.updateOrgId();
        return expStorageDeptDao.getByStorageLevel(storageDept);
    }

    /**
     * 入库详细信息
     *
     * @return.
     * @author qml
     */
    public static ExpImportMaster getExpMasterDetilByMasterId(String id) {
        if (null != id && !id.trim().equals("")) {
            return expImportMasterDao.get(id);
        } else {
            return null;
        }
    }
    /**
     * 根据ID获取科室
     * @param id
     * @return
     * @author fengyg
     */
    public static String getExpDeptById(String id) {
        if (null != id && !id.trim().equals("")) {
            ExpStorageDept expStorageDept=expStorageDeptDao.get(id);
            if(expStorageDept!=null){
                return expStorageDept.getStorageName();
            }
            else{
                return "";
            }
        } else {
            return "";
        }
    }
    /**
     * 根据传过来的入出库类型返回供货方集合
     * @param type
     * @return
     * @author fengyg
     */
    public static List getSupplierOrStorageList(String type){
       String id= UserUtils.getUser().getOrgDept().getId();
        ExpStorageDept storage = expStorageDeptDao.getByStorageCode(UserUtils.getUser().getOrgDept().getId());
        ExpSupplierCata expSupplierCata = new ExpSupplierCata();
        expSupplierCata.setDelFlag("0");
        List<ExpSupplierCata> supplierList = expSupplierCataDao.findList(expSupplierCata);
        ExpStorageDept expStorageDept = new ExpStorageDept();
        expStorageDept.setDelFlag("0");
        List<ExpStorageDept> storageList = expStorageDeptDao.findList(expStorageDept);
        if(type.equals("采购") || type.equals("0") || type.equals("退消耗品") || type.equals("4")){
            if(storage.getStorageType().equals("0")){   //总消耗品库
                return supplierList;
            }else{
                return storageList;
            }
        }else{
            return storageList;
        }
    }

    /**
     * 获取所有消耗品采购单号
     * @return
     * @author fengyg
     * @date 2017-07-18
     */
    public static List<ExpBuyPlan> getExpBuyPlanNum(){
        return expBuyPlanDao.getAllPurchaseNumber(UserUtils.getUser().getOrgDept().getId(),UserUtils.getOrgId());
    }

    /**
     * 查询未入库的采购单号
     * @return
     * @author fengyg
     * @date 2017-07-18
     */
    public static List<ExpBuyPlan> getNoImpNum(){
        return expBuyPlanDao.getNoImpNum(UserUtils.getUser().getOrgDept().getId(),UserUtils.getOrgId());
    }
}
